
#include "../GlobalClass.h"

#include "Platform.h"



#if defined(Q_OS_WIN32)   //win

bool checkOne(LPCWSTR lpName)
{
    //  创建互斥量
    HANDLE m_hMutex = CreateMutex(nullptr, FALSE, lpName );
    //  检查错误代码
    if(GetLastError() == ERROR_ALREADY_EXISTS)
    {
        //  如果已有互斥量存在则释放句柄并复位互斥量
        CloseHandle(m_hMutex);
        m_hMutex = nullptr;
        return false;
    }
    else if(nullptr == m_hMutex)
    {
        qDebug() << "初始化互斥量失败";
        return false;
    }
    return true;
}






#elif defined(Q_OS_ANDROID)   //android

//Java

/*
void showMini()
{
    QAndroidJniObject::callStaticMethod<void>(
                "com/lyy/test/MainActivity",
                "mini",
                "()V");
}

void moveToFront()
{
    QAndroidJniObject::callStaticMethod<void>(
                "com/lyy/test/MainActivity",
                "moveTaskToFrount",
                "()V");
}

void cppAssignmentJavaInt(int num)
{
    QAndroidJniObject::callStaticMethod<void>(
                "com/leamus/Java/Module"
                , "setInt", "(I)V", num);
}
int cppUseJavaToInt()
{
    return QAndroidJniObject::callStaticMethod<jint>(
                "com/leamus/Java/Module"
                , "getInt");
}
void cppAssignmentJavaString(QString str)
{
    QAndroidJniObject strTMP = QAndroidJniObject::fromString(str);
    QAndroidJniObject::callStaticMethod<void>(
                "com/leamus/Java/Module"
                , "setString", "(Ljava/lang/String;)V"
                , strTMP.object<jstring>());
}
QString cppUseJavaToString()
{
    QAndroidJniObject str = QAndroidJniObject::callStaticObjectMethod(
                "com/leamus/Java/Module"
                ,  "getString"
                ,  "()Ljava/lang/String;");
    return str.toString();
}

void keepAlive()
{

    //QAndroidJniObject::callStaticMethod<void>(
    //            "com/leamus/Java/Module"
    //            ,  "keepActive");

}
*/


#include <QtAndroid>
#include <QAndroidJniEnvironment>
#include <QAndroidJniObject>

using namespace QtAndroid;


#define CHECK_EXCEPTION() \
    if(env->ExceptionCheck())\
    {\
        qDebug() << "exception occured";\
        env->ExceptionDescribe();\
        env->ExceptionClear();\
    }
int getScreenOrientation()
{
    QAndroidJniEnvironment env;
    QAndroidJniObject activity = androidActivity();
    jint orient = activity.callMethod<jint>(
                "getRequestedOrientation"
                );
    CHECK_EXCEPTION();
    return orient;

}
void setScreenOrientation(int orient)
{
    QAndroidJniEnvironment env;
    QAndroidJniObject activity = androidActivity();

    activity.callMethod<void>(
                "setRequestedOrientation",
                "(I)V", orient);
    CHECK_EXCEPTION();

}

// 有两种方法。
// 1.
//   getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//   必须放在 Actvity 的 setContentView 调用前…… Qt 中很难操作
// 2.通过 PowerManager 来实现：
// android/os/PowerManager
//    WakeLock newWakeLock(int , String tag);
// android.os.PowerManager.WakeLock
//      void acquire();
//      void release();
//
void keepScreenWake(bool bOn = true)
{
    static QAndroidJniObject m_wakeLock;
    QAndroidJniEnvironment env;
    QAndroidJniObject activity = androidActivity();

    if(!bOn)        //关闭
    {
        if(m_wakeLock.isValid())
        {
            m_wakeLock.callMethod<void>("release");
            CHECK_EXCEPTION();
        }
        return;
    }

    QAndroidJniObject name = QAndroidJniObject::getStaticObjectField(   //获取Context
                "android/content/Context",
                "POWER_SERVICE",
                "Ljava/lang/String;"
                );
    CHECK_EXCEPTION();

    QAndroidJniObject powerService = activity.callObjectMethod(     //调用getSystemService函数
                "getSystemService",
                "(Ljava/lang/String;)Ljava/lang/Object;",
                name.object<jstring>());
    CHECK_EXCEPTION();

    QAndroidJniObject tag = QAndroidJniObject::fromString("QtJniWakeLock");

    // 10 == SCREEN_BRIGHT_WAKE_LOCK
    //  6 == SCREEN_DIM_WAKE_LOCK
    // getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    //  before setContentView() was called.
    m_wakeLock = powerService.callObjectMethod(             //调用newWakeLock
                "newWakeLock",
                "(ILjava/lang/String;)Landroid/os/PowerManager$WakeLock;",
                10,         //SCREEN_BRIGHT_WAKE_LOCK
                tag.object<jstring>()
                );
    CHECK_EXCEPTION();

    if(m_wakeLock.isValid())        //锁定
    {
        qDebug() << "屏幕常量开启中...";
        m_wakeLock.callMethod<void>("acquire");
        CHECK_EXCEPTION();
    }
    else
        qDebug() << "屏幕常量失败!";
}




#include <android/log.h>
void customAndroidMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    int prio = ANDROID_LOG_VERBOSE;
    QByteArray localMsg = msg.toLocal8Bit();
    switch (type) {
    case QtDebugMsg:
        prio = ANDROID_LOG_DEBUG;
        break;
    case QtWarningMsg:
        prio = ANDROID_LOG_WARN;
        break;
    case QtCriticalMsg:
        prio = ANDROID_LOG_INFO;
        break;
    case QtFatalMsg:
        prio = ANDROID_LOG_FATAL;
        abort();
    default:
        break;
    }
    __android_log_write(prio, "Pleafles(Android):", localMsg.data());
}

/*
void setScreenKeepWake()
{
    QAndroidJniObject::callStaticMethod<void>(
                    "com/leamus/Java/JavaFunctions"
                    , "screen_KeepWake");
}
void setScreenReleaseWake()
{
    QAndroidJniObject::callStaticMethod<void>(
                    "com/leamus/Java/JavaFunctions"
                    , "screen_ReleaseWake");
}
*/




#elif defined(Q_OS_LINUX)

//一个实例运行
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
bool checkOnly()
{
    const char filename[]  = "/tmp/lockfile";
    int fd = open (filename, O_WRONLY | O_CREAT , 0644);
    int flock = lockf(fd, F_TLOCK, 0 );
    if (fd == -1) {
            perror("open lockfile/n");
            return false;
    }
    //给文件加锁
    if (flock == -1) {
            perror("lock file error/n");
            return false;
    }
    //程序退出后，文件自动解锁
    return true;
}



#elif defined(Q_OS_MACOS)



#elif defined(Q_OS_IOS)




#else




#endif
